package de.sendorian.app.forumArchive.repository;

import java.util.List;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Repository;

import de.sendorian.app.forumArchive.domain.File;
import de.sendorian.app.forumArchive.domain.Post;
import de.sendorian.db.RepositoryBase;


@Repository
public class FileRepository extends RepositoryBase<File> {
    
    public FileRepository() {
        super(File.class);
    }


    public Post findPostByFile(File file) {
        String jpql = "select p from Post p,  IN(p.images) i, IN(p.archives) a where i.id = :fileId or a.id = :fileId";
        try {
            return (Post) getEntityManager().createQuery(jpql).setParameter("fileId", file.getId()).getSingleResult();
        } catch (EmptyResultDataAccessException e) {
            logger.debug("No Topic found for file " + file);
        }
        return null;
    }

    public List<File> findByUrl(String url) {
        return search("url", url);
    }
    
}
